package logic

import (
	"core/entity"
	"core/remote"
	"core/xyError"
	"game/component/room"
	"game/models"
	"sync"
)

type Union struct {
	sync.RWMutex
	Id      int64
	manager *UnionManager
	Rooms   map[string]*room.Room
}

func (u *Union) DismissRoom(roomId string) {
	u.Lock()
	defer u.Unlock()
	delete(u.Rooms, roomId)
}

// CreateRoom 创建房间
func (u *Union) CreateRoom(session *remote.Session, param *models.CreateRoomRequest, user *entity.User) *xyError.Error {
	// 生成房间号
	newRoom := room.NewRoom(u.manager.CreateDifferentRandomId(), param.UnionId, param.GameRule, u)
	u.Rooms[newRoom.Id] = newRoom
	return newRoom.UserEntryRoom(session, user)
}

func NewUnion(unionId int64, manager *UnionManager) *Union {
	return &Union{
		Id:      unionId,
		Rooms:   make(map[string]*room.Room),
		manager: manager,
	}
}
